GM.Name		= "RP"
GM.Author	= "Wantor, Kimax"
include( 'cl_scoreboard.lua' )
include( 'sh_SPropProtection.lua' )
include( 'DCollapsibleCategory2.lua' )
DeriveGamemode( "sandbox" )

team.SetUp( 1, "Citizen", Color( 255, 255, 0, 255 ) )
team.SetUp( 2, "Geek", Color( 0, 255, 0, 255 ) )

local meta = FindMetaTable("Entity")

function meta:Door()
	if self:GetClass() == "func_door" or
		self:GetClass() == "func_door_rotating" or
		self:GetClass() == "prop_door_rotating" then
		return true
	else
		return false
	end
end

function meta:DoorOwner()
	local Owner = self:GetNWEntity("doorowner")
	return Owner
end

function msgAPlayer(ply, text)
	if CLIENT then return end
	local playerName = ply:Name()
	local toWhom = string.Explode(" ", text)[2]
	local length = string.len("!msg "..toWhom.." ") + 1
	local message, messageTo, messageFrom
	
	for k, v in pairs(player.GetAll()) do
		if(string.find(string.lower(v:Name()), string.lower(toWhom))) then
			message = string.sub(text, length)
			messageTo = v:Name()
			messageFrom = ply:Name()
			
			ply:ConCommand("sendmessage \""..message.."\" \""..messageTo.."\"")
			v:ConCommand("recievemessage \""..message.."\" \""..messageFrom.."\"")
			
			return
		end
	end
	ply:ChatPrint("The player "..toWhom.." does not exists")
	return
end

function meta:OwnDoor(pl)
	if pl:GetMoney() < 50 then return end
	self:SetNWBool("doorowned", true)
	self:SetNWEntity("doorowner", pl)
	pl:SetNWInt("rpmoney", pl:GetMoney() - 50)
	pl:EmitSound("ambient/office/coinslot1.wav")
end

function meta:SellDoor(pl)
	self:SetNWBool("doorowned", false)
	self:SetNWEntity("doorowner", NULL)
	pl:SetNWInt("rpmoney", pl:GetMoney() + 30)
	pl:EmitSound("ambient/office/coinslot1.wav")
end

function meta:GetMoney()
	local moneys = tonumber(self:GetNWInt("rpmoney"))
	return moneys
end

function DropMoney(ply, args)
	if CLIENT then return end
	local amount = math.Round(tonumber(args))

	if amount < 1 then return end
	
	if ply:GetMoney() < amount then
		ply:ChatPrint("Not enough money!")
		return
	end

	ply:SetNWInt("rpmoney", ply:GetMoney() - amount)

	local mdl
	if amount < 100 then
		mdl = "models/money/silvcoin.mdl"
	elseif amount < 5000 then
		mdl = "models/props/cs_assault/Dollar.mdl"
	elseif amount < 10000 then
		mdl = "models/props/cs_assault/Money.mdl"
	elseif amount < 25000 then
		mdl = "models/props_c17/BriefCase001a.mdl"
	elseif amount < 50000 then
		mdl = "models/props_c17/SuitCase_Passenger_Physics.mdl"
	elseif amount < 100000 then
		mdl = "models/props_c17/SuitCase001a.mdl"
	else
		mdl = "models/money/goldbar.mdl"
	end
	
	local trace = {}
	trace.start = ply:EyePos()
	trace.endpos = trace.start + ply:GetAimVector() * 85
	trace.filter = ply

	local tr = util.TraceLine(trace)
	local money = ents.Create("prop_physics")
	money:SetModel(mdl)
	money:SetPos(tr.HitPos)
	money:SetNWString("Owner", "Shared")
	money.candupe = false
	money:Spawn()
	money:GetTable().MoneyDrop = true
	money:GetTable().Amount = amount
end